<?php

class edit_track
{
	var $mess;

	function edit_track()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if(isset($_POST['editItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select t_fullTitle from ". TRACKS_TABLE . " Where t_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". TRACKS_TABLE . " Where t_id = " . $_POST['itemID'];
			$result = $db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['t_fullTitle'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". TRACKS_TABLE . " (t_shortTitle, t_fullTitle, t_location, t_downloadURL, t_eventnames) values('" .
							$_POST['shortTitle'] . "', '" . $_POST['fullTitle'] . "', '" . $_POST['location'] . "', '" . 
                                                        $_POST['download'] ."', ' " . $_POST['eventnames'] . "')" ;
			$result = $db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', $_POST['fullTitle'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". TRACKS_TABLE . " set t_shortTitle = '" . $_POST['shortTitle'] .
													  "', t_fullTitle = '" . $_POST['fullTitle'] .
													  "', t_location = '" . $_POST['location'] .
													  "', t_downloadURL = '" . $_POST['download'] .
                                                                                                          "', t_eventnames = '" . $_POST['eventnames'] .
													  "' WHERE t_id = " . $_POST['id'] ;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['fullTitle'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
		}
		
		
		return $valid;
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select * From ". TRACKS_TABLE . " Where t_id = " . $_POST['itemID'];
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
			$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
			$mess->render();
			
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
					
			include $skin_dir . 'admin/edittrack.tpl';
		}
		else
		{
			$mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			$mess->render();
		}
	}

	function renderAdd()
	{
		global $skin_dir,  $lang;
		
		$mess = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$mess->render();
		
		$row['t_id'] = '';
		$row['t_shortTitle'] = '';
		$row['t_fullTitle'] = '';
		$row['t_location'] = '';
		$row['t_downloadURL'] = '';
                $row['t_eventnames'] = '';
		$row['t_logo'] = '';
		$row['t_logoThmb'] = '';
		
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
		
		include $skin_dir . 'admin/edittrack.tpl';
	}
	
}
?>